#include "common_opr_all.h"

#include <iostream>
#include <fstream>

CommonOprServser::CommonOprServser()
{
}

CommonOprServser::~CommonOprServser()
{
}

void CommonOprServser::getReqID(string &reqId)
{
    //得到序号
    static int seqNum = 0;
    reqId = CommonOprTime::getTimestr();

    if (seqNum < 10)
    {
        reqId = reqId + "00" + to_string(seqNum);
    }
    else if (seqNum < 100)
    {
        reqId = reqId + "0" + to_string(seqNum);
    }
    else
    {
        reqId = reqId + to_string(seqNum);
    }

    if (seqNum >= 998)
    {
        seqNum = 0;
    }
    else
    {
        seqNum += 2;
    }
}

string CommonOprServser::getTuid()
{

    if (!global_data::g_car_tuid.empty())
    {
        return global_data::g_car_tuid;
    }

    // Log_Mqtt("[CommonOprServser] [getTuid] get tuid from config file ...");

    try
    {
        string tuid;
        CommonOprSystem::readFileAllOnce(global_data::g_tuid_path, tuid);
        if (tuid.length() >= 32)
        {
            global_data::g_car_tuid = tuid.substr(0, 32);
        }
    }
    catch (const std::exception &e)
    {
        Log_ERROR("Open file failed: {}", e.what());
    }

    Log_Mqtt("[CommonOprServser] [getTuid] get tuid from config file succ, tuid: {}", global_data::g_car_tuid);
    return global_data::g_car_tuid;
}
